<!-- xxx功能1 -->
<template>
  <div id="function1">
    <div class="content-scroll">
      <SmallTitle :titleName="titleName"></SmallTitle>
      <div class="add-box">
        <Input type="text" size="large" v-model.trim="inputName" placeholder="请添加姓名" class="input-box"/>
        <Input type="text" size="large" v-model.trim="inputPhone" placeholder="请添加电话" class="input-box"/>
        <Input type="text" size="large" v-model.trim="inputPassword" placeholder="请添加密码" class="input-box"/>
        <Button type="success" @click="addInfor" class="add-btn">添加</Button>
      </div>
      <div class="search-box">
        <Input type="text" size="large" v-model="inputSearch" placeholder="请输入搜索信息" class="input-box"/>
        <Button type="success" @click="searchInfor" class="add-btn">搜索</Button>
      </div>
      <div class="table-box">
        <Table stripe border :columns="columnsData" :data="datas"></Table>
      </div>
    </div>
    <!-- 改密码弹窗 -->
    <el-dialog title="改密码" :visible.sync="dialogFormVisible" append-to-body>
      <el-form :model="form">
        <el-form-item label="用户名字" :label-width="formLabelWidth" >
          <el-input v-model="datas[indexs].name" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="用户账号" :label-width="formLabelWidth" >
          <el-input v-model="datas[indexs].phone" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="新密码" :label-width="formLabelWidth">
          <el-input type="test" v-model="form.password" placeholder="请输入新密码" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="commit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import SmallTitle from '../../parts/SmallTitle'
export default {
  name: 'function1',
  components: {
    'SmallTitle': SmallTitle
  },
  data () {
    return {
      indexs: 0,
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      form: {
        password: ''
      },
      inputSearch: '',
      inputName: '',
      inputPhone: '',
      inputPassword: '',
      titleName: '我是一个功能function1',
      columnsData: [
        {title: 'ID', key: 'id'},
        {title: '姓名', key: 'name'},
        {title: '电话', key: 'phone'},
        {title: '性别', key: 'sex'},
        {title: '邮箱', key: 'email'},
        {
          title: '操作',
          key: 'action',
          width: 130,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '10px'
                },
                on: {
                  click: () => {
                    this.dialogFormVisible = true
                    this.indexs = params.index
                  }
                }
              }, '改密'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small'
                },
                on: {
                  click: () => {
                    this.removeInfor(params.index)
                  }
                }
              }, '删除')
            ])
          }
        }
      ],
      datas: [],
      userList: []
    }
  },
  computed: {
  },
  methods: {
    commit () {
      this.axios.get('/api/admin/setpassword', {
        params: {
          id: this.datas[this.indexs].id,
          password: this.form.password
        }
      })
        .then((response) => {
          this.dialogFormVisible = false
          this.$Message.success('修改密码成功!')
        })
        .catch((error) => {
          this.$Notice.error({
            title: '通知',
            desc: '接口，异常！'
          })
          console.info(error)
        })
    },
    addInfor () {
      if (this.inputName.length === 0) {
        this.$Message.error('用户名称不可为空!')
      } else if (this.inputPhone.length === 0) {
        this.$Message.error('电话不可为空!')
      } else if (this.inputPassword.length === 0) {
        this.$Message.error('密码不可为空!')
      } else {
        this.datas.push({name: this.inputName, phone: this.inputPhone, password: this.inputPassword})
        this.$Message.success('添加用户成功!')
        this.inputName = ''
        this.inputPhone = ''
        this.inputPassword = ''
      }
    },
    removeInfor (index) {
      this.axios.get('/api/admin/delectUser', {
        params: {
          id: this.datas[index].id
        }
      })
        .then((response) => {
          this.datas = []
          this.getAllUserData()
        })
        .catch((error) => {
          this.$Notice.error({
            title: '通知',
            desc: '接口，异常！'
          })
          console.info(error)
        })
    },
    searchInfor () {
      var this_ = this
      alert(this.inputSearch)
      this.axios.get('/api/findUserMessage', {
        params: {
          phone: this.inputSearch
        }
      })
        .then(res => {
          this_.datas = []
          this_.userList = res.data.data
          console.log('user', this_.userList)
          for (var i of this_.userList) {
            this_.datas.push({id: i.id, name: i.userMessages.nickName, phone: i.phone, sex: i.userMessages.sex, email: i.userMessages.email})
          }
        })
    },
    getAllUserData () {
      this.axios.get('/api/admin/getAllUser')
        .then((response) => {
          this.userList = response.data.data
          for (var i of this.userList) {
            this.datas.push({id: i.id, name: i.userMessages.nickName, phone: i.phone, sex: i.userMessages.sex, email: i.userMessages.email})
          }
          console.log(response)
        })
      .catch((error) => {
        this.$Notice.error({
          title: '通知',
          desc: '接口，异常！'
        })
        console.info(error)
      })
    }
  },
  created () {
    this.getAllUserData()
  }
}
</script>

<style scoped>
  /*@import '../../../../static/css/Parks.css';*/
  #function1 {
    height: 100%;
    width: auto;
  }

  .content-scroll {
    height: 100%;
    width: auto;
    overflow: scroll;
    overflow-x: auto;
    background-color: white;
  }

  .add-box {
    float: left;
    padding: 15px;
  }

  .search-box {
    float: right;
    padding: 15px;
  }

  .input-box {
    width: 200px;
    float: left;
    padding-right: 15px;
  }

  .add-btn  {
    height: 35px;
    float: left;
  }

  .table-box {
    height: auto;
    width: 100%;
    float: left;
    padding: 0 15px 15px 15px;
  }
</style>
